package cn.wolfcode.rbac.controller;
// 需求： 要求请求路径为 /department/list

import cn.wolfcode.rbac.anno.RequiredPermission;
import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.qo.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    // 处理查询所有部门请求
    // 请求路径为：/department/list


    @RequestMapping("/list")
    @RequiredPermission(name = "部门查询",expression = "department:list")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }

    // 处理删除请求
    // 请求路径为：/department/delete
    @RequestMapping("/delete")
    @RequiredPermission(name = "部门删除",expression = "department:delete")
    public String delete(Long id){
        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list"; // 重新访问一次/list
    }


    // 处理 去新增 / 去修改
    // 请求路径为：/department/input
    @RequestMapping("/input")
    @RequiredPermission(name = "部门去新增或修改",expression = "department:input")
    public String input(Long id, Model model){
        if (id != null) { // 去修改
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "department/list"; //
    }

    // 处理 新增 / 修改
    // 请求路径为：/department/delete
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门新增或修改",expression = "department:saveOrUpdate")
    public String input(Department department){
        if (department.getId() == null) {
            // 新增
            departmentService.save(department);
        }else {
            // 修改
            departmentService.update(department);
        }
        return "redirect:/department/list"; // 重新访问一次/list
    }
}
